package com.ruoyi.tzzl.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.tzzl.domain.WzBalanceChangeRecord;
import com.ruoyi.tzzl.domain.WzOrder;
import com.ruoyi.tzzl.domain.WzProgramUser;
import com.ruoyi.tzzl.domain.vo.QueryVo;
import com.ruoyi.tzzl.mapper.WzBalanceChangeRecordMapper;
import com.ruoyi.tzzl.service.IWzBalanceChangeRecordService;
import com.ruoyi.tzzl.service.IWzOrderService;
import com.ruoyi.tzzl.service.IWzProgramUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 余额变更记录Service业务层处理
 * @author by caoyong
 * @date 2022/11/22
 **/
@Service
public class WzBalanceChangeRecordServiceImpl extends ServiceImpl<WzBalanceChangeRecordMapper,WzBalanceChangeRecord> implements IWzBalanceChangeRecordService {

    @Autowired
    WzBalanceChangeRecordMapper wzBalanceChangeRecordMapper;
    @Autowired
    IWzProgramUserService userService;
    @Autowired
    IWzOrderService orderService;

    /**
     * 红娘分销排行
     */
    @Override
    public List<WzBalanceChangeRecord> distributionRanking() {
        return wzBalanceChangeRecordMapper.distributionRanking();
    }

    /**
     * 用户余额记录列表
     */
    @Override
    public List<WzBalanceChangeRecord> balanceRecord(QueryVo queryVo) {
        LambdaQueryWrapper<WzBalanceChangeRecord> lqw =new LambdaQueryWrapper<>();
        if (ObjectUtils.isNotEmpty(queryVo.getWid())){
            lqw.eq(WzBalanceChangeRecord::getWid,queryVo.getWid());
        }
        if (ObjectUtils.isNotEmpty(queryVo.getType())){
            switch (queryVo.getType()){
                case "1":  //收入
                    lqw.in(WzBalanceChangeRecord::getType,1,2,3,4,5,6,15,14,16);
                    break;
                case "2":
                    lqw.in(WzBalanceChangeRecord::getType,7,8,9,10,11,12,13);
                    break; //支出
            }
        }
        lqw.orderByDesc(WzBalanceChangeRecord::getCreateTime);
        return this.list(lqw);
    }

    /**
     * 查看用户业绩信息
     *
     * @param id
     */
    @Override
    public Map<String, Object> userPerformance(Long id) {
        Map<String,Object> map =new HashMap<>();
        //查询用户信息
        WzProgramUser user =userService.getById(id);
        map.put("user",user);
        //查询用户总业绩
        Double performanceMoney =0D;
        LambdaQueryWrapper<WzBalanceChangeRecord> lwq =new LambdaQueryWrapper<>();
        lwq.eq(WzBalanceChangeRecord::getType,"1");
        lwq.eq(WzBalanceChangeRecord::getWid,id);
        List<WzBalanceChangeRecord> performanceList = this.list(lwq);
        //查询总业绩订单
        List<String > performanceOrderList =new ArrayList<>(); //总业绩订单号
       LambdaQueryWrapper<WzOrder> lwqOrder =new LambdaQueryWrapper<>();
       if (ObjectUtils.isNotEmpty(performanceList)){
           for (int i = 0; i < performanceList.size(); i++) {
               performanceMoney +=performanceList.get(i).getChangeMoney();
               performanceOrderList.add(performanceList.get(i).getOrderNumber());
           }
       }
        lwqOrder.in(WzOrder::getOrderNumber,performanceOrderList);
        List<WzOrder> performanceOrderRecord = orderService.list(lwqOrder);
        map.put("performanceOrderRecord",performanceOrderRecord); //总业绩订单记录
       map.put("performanceMoney",performanceMoney); //总业绩
        //查询本月业绩
        Double thisMonthMoney =0D;
        LambdaQueryWrapper<WzBalanceChangeRecord> lqw =new LambdaQueryWrapper<>();
        lqw.eq(WzBalanceChangeRecord::getType,"1");
        lqw.eq(WzBalanceChangeRecord::getWid,id);
        lqw.between(WzBalanceChangeRecord::getCreateTime, DateUtils.monthFirstDay(),DateUtils.monthLastDay());
        List<WzBalanceChangeRecord> thisMonthList = this.list(lqw);
        List<String> thisMonthOrder = new ArrayList<>();//本月业绩订单号
        //查询本月订单记录
        LambdaQueryWrapper<WzOrder> lqwOrder =new LambdaQueryWrapper<>();
        if (ObjectUtils.isNotEmpty(thisMonthList)){
            for (int i = 0; i < thisMonthList.size(); i++) {
                thisMonthMoney +=thisMonthList.get(i).getChangeMoney();
                thisMonthOrder.add(thisMonthList.get(i).getOrderNumber());
            }
        }
        lqwOrder.in(WzOrder::getOrderNumber,thisMonthOrder);
        List<WzOrder> thisMonthOrderRecord = orderService.list(lqwOrder);
        map.put("thisMonthOrderRecord",thisMonthOrderRecord); //查询本月业绩订单记录
        map.put("thisMonthMoney",thisMonthMoney); //本月业绩

        return map;
    }

    /**
     * 查看用户累计友币
     *
     * @param id
     */
    @Override
    public int theCumulative(Long id) {
        int count =0;
        LambdaQueryWrapper<WzBalanceChangeRecord> lqw =new LambdaQueryWrapper<>();
        lqw.in(WzBalanceChangeRecord::getType,1,2,3,4,5,6,14,15);
        lqw.eq(WzBalanceChangeRecord::getWid,id);
        List<WzBalanceChangeRecord> list =this.list(lqw);
        if (ObjectUtils.isNotEmpty(list)){
            for (int i = 0; i < list.size(); i++) {
                count += list.get(i).getChangeMoney();
            }
        }
        return count;
    }
}

